class Email < ActiveRecord::Base
  validates_presence_of :message
  validates_presence_of :sender
  validates_presence_of :subject

  def self.www(ad)
    "http://tradingonrails.com/#{ad.to_param}"
  end

  def self.sendAdMessage(adMessage,ad)
    message = "Hej!\n" +
      "Du har blivit kontaktad angående din annons, #{www(ad)} .\n" +
      "Nedan följer meddelandet:\n\n" +
      "#{adMessage.message}\n\n" +
      "Tel: #{adMessage.tel}\n" +
      "Email: #{adMessage.email}\n"
    email = Email.new
    email.created = Time.now
    email.message = message
    email.receiver = ad.user.email
    email.sender = "noreply@tradingonrails.com"
    email.sender = adMessage.email if adMessage.email.length > 0
    email.subject = "Ang: #{ad.title}"
    email.save
  end

  def self.notifyAdRemoved(ad)
    email = Email.new
    email.created = Time.now
    email.message = "Auktionen #{www(ad)} , vilken du bevakar, har tagits bort.\n"
    email.sender = "noreply@tradingonrails.com"
    email.subject = "Bud på #{ad.title}"
    ad.ad_subscriptions.each do |s|
      clone = email.clone
      clone.receiver = s.user.email
      clone.save
    end
  end

  def self.notifyAuctionBid(auction)
    email = Email.new
    email.created = Time.now
    email.message = "Ett bud har lagts på auktionen #{www(auction.ad)} vilken du bevakar.\n" +
      "Budet ligger nu på #{auction.bid}kr och vinnare är #{auction.winner.user.username}.\n"
    email.sender = "noreply@tradingonrails.com"
    email.subject = "Bud på #{auction.ad.title}"
    auction.ad.ad_subscriptions.each do |s|
      clone = email.clone
      clone.receiver = s.user.email
      clone.save
    end
  end

  def self.notifyAdBought(ad,buyer)
    sellerSubject = "Sålt: #{ad.title}"
    sellerMessage = "Hej!\n" +
      "Ditt objekt från #{www(ad)} har blivit såld för #{ad.fixed.price}kr.\n" +
      "Vänligen kontakta köparen med uppgifter om betalning, som ex. kontonummer.\n\n" +
      "Köparens uppgifter:\n" +
      "Namn: #{buyer.firstname} #{buyer.lastname}\n" +
      "Adress: #{buyer.address}, #{buyer.postalcode} #{buyer.city}\n" +
      "Email: #{buyer.email}\n" +
      "Telefonnummer: #{buyer.tel} \n\n"
    sellerEmail = Email.new
    sellerEmail.created = Time.now
    sellerEmail.message = sellerMessage
    sellerEmail.receiver = ad.user.email
    sellerEmail.sender = "noreply@tradingonrails.com"
    sellerEmail.subject = sellerSubject
    sellerEmail.save

    buyerSubject = "Köpt: #{ad.title}"
    buyerMessage = "Hej!\n" +
      "Du har köpt objektet #{www(ad)} för #{ad.fixed.price}kr.\n" +
      "Säljaren kommer att kontakta dig med uppgifter om betalning.\n\n" +
      "Säljarens uppgifter:\n" +
      "Namn: #{ad.user.firstname} #{ad.user.lastname}\n" +
      "Adress: #{ad.user.address}, #{ad.user.postalcode} #{ad.user.city}\n" +
      "Email: #{ad.user.email}\n" +
      "Telefonnummer: #{ad.user.tel} \n\n"
    buyerEmail = Email.new
    buyerEmail.created = Time.now
    buyerEmail.message = buyerMessage
    buyerEmail.receiver = buyer.email
    buyerEmail.sender = "noreply@tradingonrails.com"
    buyerEmail.subject = buyerSubject
    buyerEmail.save
  end
end
